package com.company.structural.flyweight_pattern.flyweight_factory;

import com.company.structural.flyweight_pattern.concrete_flyweitht.BlackIgoChessman;
import com.company.structural.flyweight_pattern.concrete_flyweitht.WhiteIgoChessman;
import com.company.structural.flyweight_pattern.flyweight.IgoChessman;

import java.util.Hashtable;

/**
 * 围棋棋子工厂
 * @ClassName IgoChessmanFactory
 * @Author tiezheng.han
 * @Date 2020/9/1 16:10
 **/
public class IgoChessmanFactory {

    private static IgoChessmanFactory instance = new IgoChessmanFactory();
    /**
     * 使用Hashtable来存储享元对象，充当享元池
     */
    private static Hashtable ht;

    public IgoChessmanFactory() {
        ht = new Hashtable();
        IgoChessman black,white;
        black = new BlackIgoChessman();
        ht.put("b",black);
        white = new WhiteIgoChessman();
        ht.put("w",white);
    }

    /**
     * 返回享元工厂类的唯一实例
     */
    public static IgoChessmanFactory getInstance(){
        return instance;
    }

    /**
     * 通过key获取储存在Hashtable中的享元对象
     */
    public static IgoChessman getIgoCHessman(String color){
        return (IgoChessman)ht.get(color);
    }
}
